package com.jxc.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jxc.dao.LibraryListDao;
import com.jxc.entity.Librarylist;
import com.jxc.util.PageBean;

public class LibraryListDaoImpl extends HibernateDaoSupport implements
		LibraryListDao {

	public void deleteLibraryList(Librarylist librarylist) {

		this.getHibernateTemplate().delete(librarylist);
	}

	public Librarylist getLibraryList(Librarylist librarylist) {
		return (Librarylist)this.getHibernateTemplate().get(Librarylist.class, librarylist.getId());
	}

	public PageBean getLibraryListList(final String hql,final PageBean pb) {
		this.getHibernateTemplate().execute(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query=session.createQuery(hql);
			    pb.setTotalitem(query.list().size());
			    query.setFirstResult((pb.getPageon()-1)*pb.getPagesize());
			    query.setMaxResults(pb.getPagesize());
			    pb.setList(query.list());
				return pb;
			}
		});
		return pb;
	}
	public void insertLibraryList(Librarylist librarylist) {
		this.getHibernateTemplate().save(librarylist);
	}
	public void updateLibraryList(Librarylist librarylist) {
		this.getHibernateTemplate().merge(librarylist);
	}

}
